package com.three.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.bean.Employee;
import com.three.bean.Msg;
import com.three.service.EmployeeService;

@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeService;
	
	@ResponseBody
	@RequestMapping(value="empx/{ids}",method=RequestMethod.DELETE)
	public Msg deleteEmp(@PathVariable("ids") String ids) {
		List<Integer> del_ids = new ArrayList<>();
		if (ids.contains("-")) {
			String[] str_ids = ids.split("-");
			for (String id : str_ids) {
				del_ids.add(Integer.parseInt(id));
			}
			employeeService.deleteBatch(del_ids);
		} else {
			Integer id = Integer.parseInt(ids);
			employeeService.deleteEmp(id);
		}
		return Msg.success();
	}
	
	/**
	 * 修改员工信息
	 * empId要和实体一致
	 * @param employee
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="empx/{empId}",method=RequestMethod.PUT)
	public Msg updateEmp(Employee employee) {
		employeeService.updateEmp(employee);
		return Msg.success();
	}
	
	@ResponseBody
	@RequestMapping(value="empx/{id}",method=RequestMethod.GET)
	public Msg getEmp(@PathVariable(value="id") Integer id) {
		Employee employee = employeeService.getEmp(id);
		return Msg.success().add("emp", employee);
	}
	
	/**
	 * 校验用户名
	 * @param empName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="checkuser",method=RequestMethod.POST)
	private Msg checkUser(@RequestParam(value="empName") String empName) {
		boolean flag = employeeService.checkUser(empName);
		// 
		String regName = "(^[a-zA-Z0-9-]{6,16}$)|(^[\\u2E80-\\u9FFF]{3,5}$)";
		if (!empName.matches(regName)) {
			return Msg.fail().add("va_msg", "用户名6-1文英语或者3-5中文");
		}
		if(flag) {
			return Msg.success();
		} else {
			return Msg.fail().add("va_msg", "用户名不可用");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="empx",method=RequestMethod.POST)
	private Msg saveEmp(@Valid Employee employee,BindingResult result) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (result.hasErrors()) {
			List<FieldError> fieldErrors = result.getFieldErrors();
			for (FieldError fieldError : fieldErrors) {
				map.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			return Msg.fail().add("errorField", map);
		}else {
			employeeService.saveEmp(employee);
			return Msg.success();
		}
	}
	
	@ResponseBody
	@RequestMapping("empsx")
	private Msg getEmpsWithJson(@RequestParam(value="pn",defaultValue="1")Integer pn) {
		PageHelper.startPage(pn,5);
		List<Employee> emps = employeeService.getAll();
		// 连续显示五页
		PageInfo<Employee> page =new PageInfo<Employee>(emps,5);
		return Msg.success().add("pageInfo", page);
	}

	@RequestMapping("emps")
	private String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		PageHelper.startPage(pn,5);
		List<Employee> emps = employeeService.getAll();
		// 连续显示五页
		PageInfo<Employee> page =new PageInfo<Employee>(emps,5);
		model.addAttribute("pageInfo",page);
		return "list";
	}
}
